package repository.beans;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Role;
import model.User;
import repository.UserRepository;
import repository.exceptions.RepositoryException;

@Stateless (name = "UserRepository", mappedName = "ejb/UserRepository") 
public class UserRepositoryBean extends BaseRepositoryBean<User, Long> implements UserRepository {

	@PersistenceContext (unitName = "jthr")
	private EntityManager entityManager;	
	
	public UserRepositoryBean() {
		super(User.class);
	}

	@Override
	public User getUserByUsername(String username) throws RepositoryException {
		
		for(User u:this.getAll()){
			if(u.getUserName().equals(username)){
				return u;
			}
		}
		return null;
	}

	@Override
	public List<Role> getRolesForUser(Long id) throws RepositoryException{
		
		User u=this.getById(id);
		return u.getRoles();
	
	}

	@Override
	public List<User> getAllActiveUsers() throws RepositoryException {
		List<User> users = new ArrayList<User>();
		for(User user : this.getAll()) {
			if(user.isActive() == true ){
				users.add(user);
			}
		}
		return users;
	}
	
}
